#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#define ll long long
#define pb push_back
using namespace std;
template <typename T>
void debug (T x, string sir)
{
cout << sir << " este " << x << '\n';
}
int m[5][51];
struct element
{
int nr;
int x;
int y;
};
vector<element> rasp;
void mutare (int a,int b,int c,int d)
{
if(!m[a][b])
return;
if(m[c][d])
return;
if (a==2)
{
if (m[1][b] == m[a][b])
return;
}
if (a==3)
{
if (m[4][b] == m[a][b])
return;
}
swap(m[a][b],m[c][d]);
element aux;
aux.nr = m[c][d];
aux.x = c;
aux.y = d;
rasp.pb(aux);
}
void solve()
{
int n,k;
cin >> n >> k;
for (int i=1;i<=4;i++)
for(int j=1;j<=n;j++)
cin >> m[i][j];
while(k)
{
for (int i=1;i<=n;i++)
{
if (m[2][i] && m[1][i] == m[2][i])
{
k--;
element aux;
aux.nr = m[2][i];
aux.x = 1;
aux.y = i;
rasp.pb(aux);
m[2][i] = 0;
}
if (m[3][i] && m[4][i] == m[3][i])
{
k--;
element aux;
aux.nr = m[3][i];
aux.x = 4;
aux.y = i;
rasp.pb(aux);
m[3][i] = 0;
}
}
if (k==2*n)
{
cout << "-1";
return;
}
for (int i=1;i<n;i++)
mutare(2,i,2,i+1);
mutare(2,n,3,n);
for (int i=n;i>1;i--)
mutare(3,i,3,i-1);
if (rand()%3==0 && m[3][1]!=m[4][1])
mutare(3,1,2,1);
}
cout << rasp.size() << '\n';
for (auto element : rasp)
cout << element.nr << ' ' << element.x << ' ' << element.y << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin >> t;
while (t)
{
t--;
solve();
}
return 0;
}
1343C - Alternating Subsequence | 1325A - EhAb AnD gCd |
746A - Compote | 318A - Even Odds |
550B - Preparing Olympiad | 939B - Hamster Farm |
732A - Buy a Shovel | 1220C - Substring Game in the Lesson |
452A - Eevee | 1647B - Madoka and the Elegant Gift |
1408A - Circle Coloring | 766B - Mahmoud and a Triangle |
1618C - Paint the Array | 469A - I Wanna Be the Guy |
1294A - Collecting Coins | 1227A - Math Problem |
349A - Cinema Line | 47A - Triangular numbers |
1516B - AGAGA XOOORRR | 1515A - Phoenix and Gold |
1515B - Phoenix and Puzzle | 155A - I_love_username |
49A - Sleuth | 1541A - Pretty Permutations |
1632C - Strange Test | 673A - Bear and Game |
276A - Lunch Rush | 1205A - Almost Equal |
1020B - Badge | 1353A - Most Unstable Array |